//
//  PhotoScrollView.m
//  Project1
//
//  Created by 黄珂耀 on 15/7/23.
//  Copyright (c) 2015年 黄珂耀. All rights reserved.
//

#import "PhotoScrollView.h"
#import "UIImageView+WebCache.h"

@implementation PhotoScrollView
{
    BOOL isHidden;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self _createScrollView];
    }
    return self;
}

- (void)setImageURL:(NSString *)ImageURL
{
    _imageURL = ImageURL;
    [self setNeedsLayout];
}

- (void)_createScrollView
{    
    _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:_imageView];
    
    self.delegate = self;
    
    self.tag = 100;
    
    self.maximumZoomScale = 3;
    self.minimumZoomScale = 1;
    
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap)];
    
    doubleTap.numberOfTapsRequired = 2;
    doubleTap.numberOfTouchesRequired = 1;

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
    
    [self addGestureRecognizer:singleTap];
    
    [self addGestureRecognizer:doubleTap];
    
    [singleTap requireGestureRecognizerToFail:doubleTap];
    
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _imageView;
}

- (void)singleTap
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hideNavigationBar" object:nil];
}

- (void)doubleTap
{
    if (self.zoomScale == 1)
    {
        [self setZoomScale:3 animated:YES];
    }
    else
    {
        [self setZoomScale:1 animated:YES];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    [_imageView sd_setImageWithURL:[NSURL URLWithString:_imageURL]];
//    [_imageView setImage:[UIImage imageNamed:@"1"]];
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
